package com.ciwong.epaper.modules.reciteWords.dbreciteword;


import com.ciwong.epaper.common.db.DBAsyn;
import com.ciwong.epaper.common.db.DBCallBack;
import com.ciwong.libs.utils.CWLog;

import java.util.List;

public class DBWordBookStateManagerAsyn extends BaseDBWordBookStateManager {
    private String TAG = "DBWordBookStateManagerAsyn";
    private static DBWordBookStateManagerAsyn dbManager = null;

    private DBWordBookStateManagerAsyn() {
        super();
    }

    public static DBWordBookStateManagerAsyn getInstance() {
        if (null == dbManager)
            dbManager = new DBWordBookStateManagerAsyn();
        return dbManager;
    }

    /**
     * 获取did对应数据(同步)
     *
     * @param bookId
     * @return
     */
    public void queryWordBookUrlHashCodeAsyn(final String bookId, final DBCallBack dbCallBack) {
        new DBAsyn<Integer>() {
            @Override
            protected Integer doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                    return executeGetByBookId(bookId);
            }

            @Override
            protected void onPostExecute(Integer result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();

    }
    /**
     * 操作数据库插入数据(异步)
     *
     * @param bookId
     * @param hashCode
     */
    public void addOrUpdateAsyn(String bookId, int hashCode) {
        addOrUpdateAsyn(bookId, hashCode, null);
    }

    /**
     * 操作数据库插入数据(异步)
     *
     * @param bookId
     * @param hashCode
     * @param dbCallBack
     */
    public void addOrUpdateAsyn(final String bookId, final int hashCode, final DBCallBack dbCallBack) {
        new DBAsyn<Boolean>() {
            @Override
            protected Boolean doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                try{
                    executeAddOrUpdateData(bookId, hashCode, dbCallBack);
                }catch (Exception e){
                    return false;
                }
                return true;
            }

            @Override
            protected void onPostExecute(Boolean result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();


    }

    public void  deleteAllAsny(final String bookId,final  DBCallBack dbCallBack) {
        new DBAsyn<Boolean>() {
            @Override
            protected Boolean doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                try{
                    executeDeleteAll();
                }catch (Exception e){
                    return false;
                }
                return true;
            }

            @Override
            protected void onPostExecute(Boolean result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();

    }

    public void queryAllAsny(final String bookId, final DBCallBack dbCallBack) {
        new DBAsyn<List<Integer>>() {
            @Override
            protected  List<Integer> doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                    return  executeQueryAll();
            }

            @Override
            protected void onPostExecute( List<Integer> result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();
    }
}
